package com.javaee.weixin.xiaochengxu.tools;

import java.io.IOException;
import java.util.Iterator;
import java.util.Map;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.SSLSession;

import okhttp3.CertificatePinner;
import okhttp3.Headers;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

/**
 * 
 * OKHTTP3 调用https忽略证验证<br>
 * 
 * @author songzx
 * @version 1.0,2017年4月22日 下午2:45:48
 * @since wxapi 0.0.1
 */
public class OkHttp3NoVerifierSSLUtil {

	/**
	 * Instantiates a new ok http 3 no verifier SSL util.
	 */
	private OkHttp3NoVerifierSSLUtil() {
	}

	/** The Constant MediaType_JSON. */
	public final static MediaType MediaType_JSON = MediaType.parse("application/json; charset=UTF-8");
	public final static MediaType MediaType_XML = MediaType.parse("application/xml; charset=UTF-8");

	/** The Constant HEADERS_JSON. */
	public final static Headers HEADERS_JSON = Headers.of("Context-Type", "application/json;charset=UTF-8");
	
	public final static Headers HEADERS_XML = Headers.of("Context-Type", "application/xml;charset=UTF-8");

	/** The Constant HEADERS_HTML. */
	public final static Headers HEADERS_HTML = Headers.of("Context-Type", "text/html;charset=UTF-8");

	/** The ok http 3 no verifier SSL util. */
	private static OkHttp3NoVerifierSSLUtil okHttp3NoVerifierSSLUtil;

	/** The client. */
	private static OkHttpClient client;

	/**
	 * Gets the single instance of OkHttp3NoVerifierSSLUtil.
	 *
	 * @return single instance of OkHttp3NoVerifierSSLUtil
	 */
	public static OkHttp3NoVerifierSSLUtil getInstance() {
		if (okHttp3NoVerifierSSLUtil == null) {
			okHttp3NoVerifierSSLUtil = new OkHttp3NoVerifierSSLUtil();

			OkHttpClient.Builder builder = new OkHttpClient.Builder();
			builder.certificatePinner(CertificatePinner.DEFAULT);
			HostnameVerifier hostnameVerifier = new HostnameVerifier() {
				public boolean verify(String arg0, SSLSession arg1) {
					return true;
				}
			};
			builder.hostnameVerifier(hostnameVerifier);
			client = builder.build();
		}
		return okHttp3NoVerifierSSLUtil;
	}

	/**
	 * Gets the.
	 *
	 * @param headers
	 *            the headers
	 * @param httpsUrl
	 *            the https url
	 * @return the response
	 * @throws IOException
	 *             Signals that an I/O exception has occurred.
	 */
	public Response get(Headers headers, String httpsUrl) throws IOException {
		Request request = new Request.Builder().headers(headers).url(httpsUrl).build();
		Response response = client.newCall(request).execute();
		return response;
	}

	/**
	 * Post.
	 *
	 * @param headers
	 *            the headers
	 * @param httpsUrl
	 *            the https url
	 * @param body
	 *            the body
	 * @return the response
	 * @throws IOException
	 *             Signals that an I/O exception has occurred.
	 */
	public Response post(Headers headers, String httpsUrl, RequestBody body) throws IOException {
		Request request = new Request.Builder().headers(headers).url(httpsUrl).post(body).build();
		Response response = client.newCall(request).execute();
		return response;
	}

	/**
	 * Gets the url.
	 *
	 * @param url
	 *            the url
	 * @param path
	 *            the path
	 * @return the url
	 */
	public static String getUrl(String url, Map<String, String> path) {
		for (Iterator<String> it = path.keySet().iterator(); it.hasNext();) {
			String key = it.next();
			url = url.replace(String.format("{%s}", key.toUpperCase()), path.get(key));
		}
		return url;
	}

}
